<template>
	<view class="page-body">
		<view class="tabr" :style="{top:headerTop}">
			<view :class="{on:typeClass=='valid'}" @tap="switchType('valid')">可用({{couponValidList.length}})</view><view :class="{on:typeClass=='invalid'}"  @tap="switchType('invalid')">已失效</view>
			<view class="border" :class="typeClass"></view>
		</view>
		<view class="place" ></view>
		<view class="list">
			<!-- 优惠券列表 -->
			<view class="sub-list valid" :class="subState">
				<view class="tis" v-if="couponValidList.length==0">没有数据~</view>
				<view class="row" v-for="(row,index) in couponValidList" :key="index" @click="toGoodslist">
					<!-- content -->
					<view class="carrier" :class="[typeClass=='valid'?theIndex==index?'open':oldIndex==index?'close':'':'']" >
						<view class="left">
							<view class="title">
								{{row.title}}
							</view>
							<view class="term" style="color: #666;">专属：xxxxx商品</view>
							<view class="term">
								{{row.create_time}} ~ {{row.end_time}}
							</view>
							<view class="gap-top"></view>
							<view class="gap-bottom"></view>
						</view>
						<view class="right flex">
							<view class="use">
								去使用
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="sub-list invalid" :class="subState">
				<view class="tis" v-if="couponinvalidList.length==0">没有数据~</view>
				<view class="row" v-for="(row,index) in couponinvalidList" :key="index" >
					<!-- content -->
					<view class="carrier" :class="[typeClass=='invalid'?theIndex==index?'open':oldIndex==index?'close':'':'']" >
						<view class="left">
							<view class="title">
								{{row.title}}
							</view>
							<view class="term" style="color: #666;">专属：xxxxx商品</view>
							<view class="term">
								{{row.create_time}} ~ {{row.end_time}}
							</view>
							<view class="icon shixiao"></view>
							<view class="gap-top"></view>
							<view class="gap-bottom"></view>
						</view>
						<view class="right invalid flex">
							<view class="use">
								去使用
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
var that;
import {pageLoad} from '@/utils/request.js';
	export default {
		data() {
			return {
				couponValidList:[
					{id:1,title:"日常用品立减10元",termStart:"2019-04-01",termEnd:"2019-05-30",ticket:"10",criteria:"满50使用"},
					{id:2,title:"家用电器立减100元",termStart:"2019-04-01",termEnd:"2019-05-30",ticket:"100",criteria:"满500使用"},
					{id:3,title:"全场立减10元",termStart:"2019-04-01",termEnd:"2019-05-30",ticket:"10",criteria:"无门槛"},
					{id:4,title:"全场立减50元",termStart:"2019-04-01",termEnd:"2019-05-30",ticket:"50",criteria:"满1000使用"}
					
				],
				couponinvalidList:[
					// {id:1,title:"日常用品立减10元",termStart:"2019-04-01",termEnd:"2019-05-30",ticket:"10",criteria:"满50使用"},
					// {id:2,title:"家用电器立减100元",termStart:"2019-04-01",termEnd:"2019-05-30",ticket:"100",criteria:"满500使用"},
					// {id:3,title:"全场立减10元",termStart:"2019-04-01",termEnd:"2019-05-30",ticket:"10",criteria:"无门槛"},
					// {id:4,title:"全场立减50元",termStart:"2019-04-01",termEnd:"2019-05-30",ticket:"50",criteria:"满1000使用"}
				],
				headerTop:0,
				//控制滑动效果
				typeClass:'valid',
				subState:'',
				theIndex:null,
				oldIndex:null,
				isStop:false,
				typeid:0,
				listData:"",
				page:1,
				Tab:[{name:"可用",id:0},{name:"已失效",id:0}],
			}
		},
		onPageScroll(e){
			
		},
		//下拉刷新，需要自己在page.json文件中配置开启页面下拉刷新 "enablePullDownRefresh": true
		onPullDownRefresh() {
		    setTimeout(function () {
		        uni.stopPullDownRefresh();
		    }, 1000);
		},
		onLoad() {
			that=this;
			//兼容H5下排序栏位置
			// #ifdef H5
				//定时器方式循环获取高度为止，这么写的原因是onLoad中head未必已经渲染出来。
				let Timer = setInterval(()=>{
					let uniHead = document.getElementsByTagName('uni-page-head');
					if(uniHead.length>0){
						this.headerTop = uniHead[0].offsetHeight+'px';
						clearInterval(Timer);//清除定时器
					}
				},1);
			// #endif
			this.tabInit()
		},
		methods: {
			tabInit(){
				let Tab = this.Tab;
				Tab.forEach(item=>{
					item.listData = [];
					item.loadtype = 0;  //加载更多 0加载前，1加载中，2没有更多了
					item.refreshing = 0;
					item.listnull=false;
					item.listLen=10;
					item.text2=""
					item.page=1;
					item.loaded=false
				})	
				that.listItems=Tab;
				that.getCouponList(true)
			},
			getCouponList(isReload){
				let tabindex=this.typeid;
				let list=this.listItems[tabindex];
				// let tabid=list.id;
				if(isReload){
					list.listData=[];
					list.loadtype=0;
					list.listnull=false;
					list.page=1;
					that.$set(that.listItems,tabindex,list);
				}
				if (list.loadtype==2||list.loadtype==1) { return; }
				this.$https.POST({
					url:"/user/userCoupons",
					data:{
						type:tabindex,//1失效 0可用
					},
					success(res){
						list.loadtype=1;
						let nowlistData=list.listData;
						res.data.forEach((item,index)=>{
							
						})
						let listData=nowlistData.concat(res.data)
						list.listData=listData;
						++list.page;
						if(list.listData.length==0&&res.data.length==0){
							list.listnull=true
						}
						if(list.listData.length>0&&res.data.length<10){
							if(list.listData.length<=list.listLen){
								list.text2=" ";
							}
							list.loadtype=2;
						   list.loadtype=2;
						   that.$set(that.listItems,tabindex,list)
						   return;
						}
						that.$set(that.listItems,tabindex,list)
						list.loadtype=0;
					}
				}).then(()=>{
					this.initShowData(this.typeid)
				})
			},
			initShowData(typeid){
				// that.getCouponList(true)
				if(typeid == 0){
					this.couponValidList = this.listItems[typeid].listData;
				}else{
					this.couponinvalidList = this.listItems[typeid].listData;
				}
			},
			switchType(type){
				let typeid = type=="valid"?'0':'1';
				this.typeid = typeid;
				if(this.typeClass==type){
					return ;
				}
				this.getCouponList(true)
				uni.pageScrollTo({
					scrollTop:0,
					duration:0
				})
				this.typeClass = type;
				this.subState = this.typeClass==''?'':'show'+type;
				setTimeout(()=>{
					this.oldIndex = null;
					this.theIndex = null;
					this.subState = this.typeClass=='valid'?'':this.subState;
				},200)
			},
			discard() {
				//丢弃
			},
			toGoodslist(){
				uni.navigateTo({
					url:"/pages/goods/goods-list/goods-list"
				})
			}
		},
		onReachBottom(){
			this.getCouponList(true)
		},
		onUnload(){
			that = null;
		}
	}
</script>
<style lang="scss">
	view{
		display: flex;
		flex-wrap: wrap;
		
	}
	page{position: relative;background-color: #f6f6f6;}
	.hidden{
		display: none !important;
	}
	.place{
		width: 100%;
		height: 95upx;
	}
	.tabr{
		background-color: #fff;
		width: 94%;
		height: 95upx;
		padding: 0 3%;
		border-bottom: solid 1upx #eee;
		position: fixed;
		top: 0;
		z-index: 10;
		view{
			width: 50%;
			height: 90upx;
			justify-content: center;
			align-items: center;
			font-size: 32upx;
			color: #999;
		}
		.on{
			color: $maincolor;
		}
		.border{
			height: 4upx;
			background-color: $maincolor;
			transition: all .3s ease-out;
			&.invalid{
				transform: translate3d(100%,0,0);
			}
		}
		
	}
	.list{
		width: 100%;
		display: block;
		position: relative;
	}
	@keyframes showValid {
		0% {transform: translateX(-100%);}100% {transform: translateX(0);}
	}
	@keyframes showInvalid {
		0% {transform: translateX(0);}100% {transform: translateX(-100%);}
	}
	.sub-list{
		&.invalid{
			position: absolute;
			top: 0;
			left:100%;
			display: none;
		}
		&.showvalid{
			display: flex;
			animation: showValid 0.20s linear both;
		}
		&.showinvalid{
			display: flex;
			animation: showInvalid 0.20s linear both;
		}
		width: 100%;
		padding: 20upx 0 120upx 0;
		.tis{
			width: 100%;
			height: 60upx;
			justify-content: center;
			align-items: center;
			font-size: 32upx;
		}
		.row{
			width: 92%;
			height: 24vw; 
			margin: 20upx auto 10upx auto;
			border-radius: 8upx;
			// box-shadow: 0upx 0 10upx rgba(0,0,0,0.1);
			align-items: center;
			position: relative;
			overflow: hidden;
			z-index: 4;
			border: 0;
			.menu{
				.icon{
					color: #fff;
					font-size:50upx;
				}
				position: absolute;
				width: 28%;
				height: 100%;
				right: 0;
				justify-content: center;
				align-items: center;
				background-color: red;
				color: #fff;
				z-index: 2;
			}
			.carrier{
				@keyframes showMenu {
					0% {transform: translateX(0);}100% {transform: translateX(-28%);}
				}
				@keyframes closeMenu {
					0% {transform: translateX(-28%);}100% {transform: translateX(0);}
				}
				&.open{
					animation: showMenu 0.25s linear both;
				}
				&.close{
					animation: closeMenu 0.15s linear both;
				}
				background-color: #fff;
				position: absolute;
				width: 100%;
				padding: 0 0;
				height: 100%;
				z-index: 3;
				flex-wrap: nowrap;
				.left{
					width: 100%;
					.title{
						padding-top: 3vw;
						width: 90%;
						margin: 0 5%;
						font-size: 36upx;
					}
					.term{
						width: 90%;
						margin: 0 5%;
						font-size: 26upx;
						color: #999;
					}
					position: relative;
					.gap-top,.gap-bottom{
						position: absolute;
						width: 20upx;
						height: 20upx;
						right: -10upx;
						border-radius: 100%;
						background-color: #f5f5f5;
					}
					.gap-top{
						top: -10upx;
					}
					.gap-bottom{
						bottom: -10upx;
					}
					.shixiao{
						position: absolute;
						right: 20upx;
						font-size: 150upx;
						z-index: 6;
						color: rgba(153,153,153,0.2)
					}
				}
				.right{
					flex-shrink: 0;
					width: 28%;
					color: #fff;
					background:linear-gradient(to right,$maincolor,#fb7b7b);
					&.invalid{
						background:linear-gradient(to right,#aaa,#999);
						.use{
							color: #aaa;
						}
					}
					justify-content: center;
					.ticket,.criteria{width: 100%;}
					.ticket{
						padding-top: 1vw;
						justify-content: center;
						align-items: baseline;
						height: 6vw;
						.num{
							font-size: 42upx;
							font-weight: 600;
						}
						.unit{
							font-size: 24upx;
						}
					}
					.criteria{
						justify-content: center;
						font-size: 28upx;
					}
					.use{
						width: 60%;
						height: 50upx;
						justify-content: center;
						align-items: center;
						font-size: 24upx;
						background-color: #fff;
						color: $maincolor;
						border-radius: 25upx;
						padding: 0 10upx;
					}
				}
			}
			/*
			<view class="carrier" :class="[theIndex==index?'open':oldIndex==index?'close':'']" @touchstart="touchStart(index,$event)" @touchmove="touchMove(index,$event)" @touchend="touchEnd(index,$event)">
				<view class="left">
					<view class="title">
						10元日常用品类
					</view>
					<view class="term">
						2019-04-01~2019-05-30
					</view>
				</view>
				<view class="right">
					<view class="ticket">
						<view class="num">
							10
						</view>
						<view class="unit">
							元
						</view>
					</view>
					<view class="criteria">
						满50使用
					</view>
					<view class="use">
						去使用
					</view>
				</view>
			</view>
			* 
			* */
		}
	}
	
</style>
